<template>
  <div class="login-container">
    <div class="login-content">
      <!-- 头部 -->
      <div class="login-header">
        <el-button 
          icon="ArrowLeft" 
          circle 
          @click="goBack"
          class="back-btn"
        />
        <h1 class="login-title">登录</h1>
      </div>

      <!-- 登录表单 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        @submit.prevent="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="User"
            size="large"
            clearable
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            size="large"
            show-password
            clearable
            @keyup.enter="handleLogin"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            size="large"
            class="login-btn"
            :loading="loading"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 其他操作 -->
      <div class="login-actions">
        <el-link type="primary" @click="forgotPassword">忘记密码？</el-link>
        <el-link type="primary" @click="goToRegister">还没有账号？立即注册</el-link>
      </div>

      <!-- 快速登录 -->
      <div class="quick-login">
        <p class="quick-login-title">快速登录</p>
        <div class="quick-login-buttons">
          <el-button 
            circle 
            size="large"
            @click="quickLogin('demo')"
            :loading="quickLoading === 'demo'"
          >
            演示
          </el-button>
          <el-button 
            circle 
            size="large"
            @click="quickLogin('guest')"
            :loading="quickLoading === 'guest'"
          >
            游客
          </el-button>
        </div>
      </div>
    </div>

    <!-- 忘记密码对话框 -->
    <el-dialog
      v-model="forgotPasswordVisible"
      title="忘记密码"
      width="90%"
      :close-on-click-modal="false"
    >
      <el-form
        ref="resetFormRef"
        :model="resetForm"
        :rules="resetRules"
        label-width="80px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="resetForm.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="resetForm.email" placeholder="请输入邮箱" />
        </el-form-item>
      </el-form>
      
      <template #footer>
        <el-button @click="forgotPasswordVisible = false">取消</el-button>
        <el-button type="primary" @click="handleResetPassword" :loading="resetLoading">
          发送重置邮件
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { useUserStore } from '@/stores/user'

// 响应式数据
const loading = ref(false)
const quickLoading = ref<string>('')
const forgotPasswordVisible = ref(false)
const resetLoading = ref(false)

// 表单引用
const loginFormRef = ref<FormInstance>()
const resetFormRef = ref<FormInstance>()

// 登录表单
const loginForm = reactive({
  username: '',
  password: ''
})

// 重置密码表单
const resetForm = reactive({
  username: '',
  email: ''
})

// 表单验证规则
const loginRules: FormRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

const resetRules: FormRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ]
}

// 获取路由和用户store
const router = useRouter()
const userStore = useUserStore()

// 返回上一页
const goBack = () => {
  router.back()
}

// 处理登录
const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    
    loading.value = true
    await userStore.login(loginForm)
    
    ElMessage.success('登录成功')
    router.push('/')
  } catch (error: any) {
    console.error('登录失败:', error)
    ElMessage.error(error.message || '登录失败')
  } finally {
    loading.value = false
  }
}

// 快速登录
const quickLogin = async (type: string) => {
  quickLoading.value = type
  
  try {
    let credentials
    switch (type) {
      case 'demo':
        credentials = { username: 'demo', password: '123456' }
        break
      case 'guest':
        credentials = { username: 'guest', password: '123456' }
        break
      default:
        return
    }
    
    await userStore.login(credentials)
    ElMessage.success('登录成功')
    router.push('/')
  } catch (error: any) {
    console.error('快速登录失败:', error)
    ElMessage.error(error.message || '登录失败')
  } finally {
    quickLoading.value = ''
  }
}

// 跳转到注册页
const goToRegister = () => {
  router.push('/register')
}

// 忘记密码
const forgotPassword = () => {
  forgotPasswordVisible.value = true
}

// 处理重置密码
const handleResetPassword = async () => {
  if (!resetFormRef.value) return
  
  try {
    await resetFormRef.value.validate()
    
    resetLoading.value = true
    // 这里应该调用重置密码API
    ElMessage.success('重置邮件已发送，请查收邮箱')
    forgotPasswordVisible.value = false
  } catch (error: any) {
    console.error('重置密码失败:', error)
    ElMessage.error(error.message || '重置密码失败')
  } finally {
    resetLoading.value = false
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-content {
  background: white;
  border-radius: 20px;
  padding: 40px 30px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
  position: relative;
}

.back-btn {
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
}

.login-title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.login-form {
  margin-bottom: 20px;
}

.login-btn {
  width: 100%;
  height: 50px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 25px;
  background: linear-gradient(135deg, #ff6b6b 0%, #4ecdc4 100%);
  border: none;
}

.login-actions {
  display: flex;
  justify-content: space-between;
  margin-bottom: 30px;
  font-size: 14px;
}

.quick-login {
  text-align: center;
}

.quick-login-title {
  font-size: 14px;
  color: #666;
  margin-bottom: 15px;
}

.quick-login-buttons {
  display: flex;
  justify-content: center;
  gap: 20px;
}

// 移动端适配
@media screen and (max-width: 768px) {
  .login-container {
    padding: 15px;
  }
  
  .login-content {
    padding: 30px 20px;
  }
  
  .login-title {
    font-size: 20px;
  }
  
  .login-btn {
    height: 45px;
    font-size: 14px;
  }
  
  .login-actions {
    flex-direction: column;
    gap: 10px;
    align-items: center;
  }
}
</style> 